package TreeSet.projeect;

public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    //重写该方法：根据数据内容自定
    public int compareTo(Student o) {
        int c1 = this.name.compareTo(o.name);//判断name是否相同，相同返回0
        int c2 = this.age - o.age;//结果为负，说明年龄小，为正，说明年龄大；结果为0，年龄一样
        return c1 == 0 ? c2:c1;//先判断c1是否为0，若为0，在判断年龄，若都为0，则返回0（重复了），反之，不重复。
    }
}
